//
//  OSTweetViewController.m
//  OSChina
//
//  Created by 林涛 on 14/12/24.
//  Copyright (c) 2014年 林涛. All rights reserved.
//

#import "OSTweetViewController.h"
#import "OSTableViewDataSourceObject.h"
#import "OSTweetTableViewCell.h"
#import "OSNetworkRequest.h"
@interface OSTweetViewController ()
@property (nonatomic, strong) NSMutableArray *tweetArray;
@property (strong, nonatomic) OSTableViewDataSourceObject  *tableViewDataSourceObject;
@property (assign, nonatomic) NSInteger pageCount;
@property (strong, nonatomic) IBOutlet UISegmentedControl *segmentedControl;
@end

@implementation OSTweetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setup];
}

- (void)setup
{
    self.tweetArray = [NSMutableArray array];
    TableViewCellConfigureBlock block = ^(OSTweetTableViewCell *cell, id item,NSIndexPath *indexPath){
        [cell configWith:item];
    };
    self.tableViewDataSourceObject    = [[OSTableViewDataSourceObject alloc]initWithItems:self.tweetArray cellIdentifier:[OSTweetTableViewCell identifier] configureCellBlock:block];
    self.tableView.dataSource         = self.tableViewDataSourceObject;
    [super setup];
}

- (void)headerRereshing
{
    self.pageCount = 20;
    [self.tweetArray removeAllObjects];
    [self requestNews];
}

- (void)footerRereshing
{
    [self requestNews];
}

- (void)requestNews
{
    NSString *catalog = [NSString stringWithFormat:@"%ld",self.segmentedControl.selectedSegmentIndex + 1];
    NSString *pageIndex = [NSString stringWithFormat:@"%ld",self.pageCount / 20];
    
    [OSNetworkRequest tweetLisRequestWithCount:@"20" catalog:@"-1" pageIndex:pageIndex success:^(id result) {
        NSLog(@"%@",result);
    } failure:^(NSError *error) {
        
    }];
    
//    [OSNetworkRequest answerLisRequestWithCount:@"20" catalog:catalog pageIndex:pageIndex success:^(id result) {
//        self.pageCount += 20;
//        NSDictionary *dic = result[@"posts"];
//        [self.tweetArray addObjectsFromArray:dic[@"post"]];
//        self.tableViewDataSourceObject.items = self.tweetArray;
//        [self refreshTableView];
//    } failure:^(NSError *error) {
//        NSLog(@"%@",error.localizedDescription);
//    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
